<?php
class coupon extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$begin_time = (isset($_GET['begin_time'])) ? trim($_GET['begin_time']) : '';
		$end_time = (isset($_GET['end_time'])) ? trim($_GET['end_time']) : '';
		if (strlen($begin_time)) {
			$where .= " AND begin_time>='".strtotime($begin_time)."'";
		}
		if (strlen($end_time)) {
			$where .= " AND end_time<='".strtotime($end_time)."'";
		}
		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR coupon_money LIKE '%{$keyword}%' )";
		}
		if ($id) {
			$where .= "AND id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'begin_time'=>$begin_time,'end_time'=>$end_time,'keyword'=>$keyword));

		$sql = "SELECT *  FROM jf_coupon WHERE 1 {$where} ORDER BY id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		if($rs){
			foreach ($rs as $key => $va) {
				$a =$va['id'];
				$sql = "SELECT count(*) FROM jf_coupon_sn WHERE coupon_id= {$a}";
				$sn = $this->db->get_var($sql);
				$rs[$key]['sn'] = $sn;
			}
		}
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('end_time',$end_time);
		$this->smarty->assign('begin_time',$begin_time);
		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_coupon WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$sql = "SELECT count(*) FROM jf_coupon_sn WHERE coupon_id='{$id}'";
			$sn = $this->db->get_var($sql);
			$sql = "SELECT c.id as cid ,g.name ,g.id as gid FROM jf_coupon_goods c LEFT JOIN jf_goods g ON c.goods_id = g.id 
					 WHERE c.coupon_id='{$id}'";
			$goods = $this->db->get_results($sql);
			$this->smarty->assign('goods', $goods);
			$this->smarty->assign('sn', $sn);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}
	
	//写入到数据库
	public function save() {
		$id = (isset($_POST['coupon']) && intval($_POST['coupon'])) ? intval($_POST['coupon']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$coupon_money = (isset($_POST['coupon_money']) && trim($_POST['coupon_money'])) ? trim($_POST['coupon_money']) : '';
		$position = (isset($_POST['position']) && trim($_POST['position'])) ? trim($_POST['position']) : '';
		$min_price = (isset($_POST['min_price']) && trim($_POST['min_price'])) ? trim($_POST['min_price']) : '';
		$begin_time = (isset($_POST['begin_time']) && trim($_POST['begin_time'])) ? trim($_POST['begin_time']) : '';
		$end_time = (isset($_POST['end_time']) && trim($_POST['end_time'])) ? trim($_POST['end_time']) : '';
		$quantity = (isset($_POST['quantity']) && trim($_POST['quantity'])) ? trim($_POST['quantity']) : '';
		$num_per_person = (isset($_POST['num_per_person']) && trim($_POST['num_per_person'])) ? trim($_POST['num_per_person']) : '';
		$offline_use = (isset($_POST['offline_use']) && trim($_POST['offline_use'])) ? trim($_POST['offline_use']) : '';
		$permit_goods = (isset($_POST['permit_goods']) && trim($_POST['permit_goods'])) ? trim($_POST['permit_goods']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$goods = (isset($_POST['goods']) && intval($_POST['goods'])) ? $_POST['goods'] : '';
		$begin_time = strtotime($begin_time);
		$end_time = strtotime($end_time);

		if($permit_goods == 0){
			$goods ="";
		}else if(!$goods){
			$permit_goods = 0;
		}
		if ($id > 0) {  //edit
			$sql = "UPDATE jf_coupon SET name='{$name}', coupon_money='{$coupon_money}', position='{$position}' , 
					min_price='{$min_price}', begin_time='{$begin_time}' , end_time='{$end_time}', quantity='{$quantity}'  
					, num_per_person='{$num_per_person}', status='{$status}', offline_use='{$offline_use}', 
					permit_goods='{$permit_goods}' WHERE id='{$id}'";
			$this->db->query($sql);
			//更新
			$sql = "DELETE FROM jf_coupon_goods WHERE coupon_id='{$id}'";
			$this->db->query($sql);
			$sql = "INSERT INTO jf_coupon_goods(coupon_id,goods_id) VALUES ";
        	if($goods!=0){
            foreach ($goods as $goods) {
                $sql .= "('{$id}','{$goods}'),";
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $this->db->query($sql);
        	}

        	if($quantity > 0){
        		$sql = "INSERT INTO jf_coupon_sn(coupon_id,coupon_money,sn,add_time,status) VALUES ";
        		for ($i=0; $i < $quantity ; $i++) { 
        			$sn =date('m') . date('d') . substr(time(), -3) . substr(microtime(), 2, 6) .rand(100, 999);
        			$sql .= "('{$id}','{$coupon_money}','{$sn}','".time()."',1),";
        		}
        		$sql = substr($sql, 0, strlen($sql) - 1);
            	$this->db->query($sql);
        	}
			
		} else { //add
			$sql = "INSERT INTO jf_coupon(name, coupon_money, position ,min_price,begin_time,end_time,quantity,
					num_per_person, add_time, status,offline_use,permit_goods) 
					VALUES('{$name}', '{$coupon_money}','{$position}', '{$min_price}', '{$begin_time}',
					'{$end_time}','{$quantity}','{$num_per_person}', '".time()."', '{$status}', 
					'{$offline_use}', '{$permit_goods}')";
			$this->db->query($sql);
			$insert_id = $this->db->insert_id;
			$sql = "INSERT INTO jf_coupon_goods(coupon_id,goods_id) VALUES ";
        	if($goods!=0 && $insert_id !== ''){
            foreach ($goods as $goods) {
                $sql .= "('{$insert_id}','{$goods}'),";
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $this->db->query($sql);
        	}
        	if($quantity > 0){
        		$sql = "INSERT INTO jf_coupon_sn(coupon_id,coupon_money,sn,add_time,status) VALUES ";
        		for ($i=0; $i < $quantity ; $i++) { 
        			$sn =date('m') . date('d') . substr(time(), -3) . substr(microtime(), 2, 6) . rand(100, 999);
        			$sql .= "('{$insert_id}','{$coupon_money}','{$sn}','".time()."',1),";
        		}
        		$sql = substr($sql, 0, strlen($sql) - 1);
            	$this->db->query($sql);
        	}

		}
		header("Location:?app=coupon&act=index");
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_coupon WHERE id='{$id}'";
		$this->db->query($sql);
		$sql = "DELETE FROM jf_coupon_goods WHERE coupon_id='{$id}'";
		$this->db->query($sql);
		$sql = "DELETE FROM jf_coupon_sn WHERE coupon_id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=coupon&act=index");
	}


	//sn
	public function sn(){
		$where = '';
		$coupon_id = (isset($_GET['coupon_id']) && trim($_GET['coupon_id'])) ? intval($_GET['coupon_id']) : 0;
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$status = (isset($_GET['status']) && trim($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$get_time1 = (isset($_GET['get_time1'])) ? trim($_GET['get_time1']) : '';
		$get_time2 = (isset($_GET['get_time2'])) ? trim($_GET['get_time2']) : '';
		$use_time1 = (isset($_GET['use_time1'])) ? trim($_GET['use_time1']) : '';
		$use_time2 = (isset($_GET['use_time2'])) ? trim($_GET['use_time2']) : '';
		if (strlen($get_time1)) {
			$where .= " AND get_time>='".strtotime($get_time1)."'";
		}
		if (strlen($get_time2)) {
			$where .= " AND get_time<='".strtotime($get_time2)."'";
		}
		if (strlen($use_time1)) {
			$where .= " AND use_time>='".strtotime($use_time1)."'";
		}
		if (strlen($use_time2)) {
			$where .= " AND use_time<='".strtotime($use_time2)."'";
		}
		if ($id) {
			$where .= "or s.id='{$id}' or m.id = '{$id}'";
		}
		if ($status) {
			$where .= "AND s.status='{$status}'";
		}
		if ($keyword) {
			$where .= "AND (s.sn LIKE '%{$keyword}%' OR s.id LIKE '%{$keyword}%' 
				OR s.member_id LIKE '%{$keyword}%'OR m.name LIKE '%{$keyword}%')";
		}
		//分页
		$this->setpages(array( 'coupon_id'=>$coupon_id,'status'=>$status,'keyword'=>$keyword,'get_time1'=>$get_time1,
								'get_time2'=>$get_time2,'use_time1'=>$use_time1,'use_time2'=>$use_time2));

		$sql = "SELECT s.*,m.name ,m.nick_name FROM jf_coupon_sn s LEFT JOIN jf_member m ON s.member_id = m.id 
				WHERE coupon_id = {$coupon_id} {$where} ORDER BY s.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('get_time1',$get_time1);
		$this->smarty->assign('get_time2',$get_time2);
		$this->smarty->assign('use_time1',$use_time1);
		$this->smarty->assign('use_time2',$use_time2);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('coupon_id',$coupon_id);
		$this->display();
	}

	//delete
	public function delete_sn() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_coupon_sn WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=coupon&act=sn");
	}

	//coupon_grant
	public function grant(){
		if(IS_POST){
			$thing = "";
			$all = (isset($_POST['all']) && trim($_POST['all'])) ? intval($_POST['all']) : 1;
			$member_id = (isset($_POST['member_id']) && trim($_POST['member_id'])) ? intval($_POST['member_id']) : 0;
			$coupon_id = (isset($_POST['coupon_id']) && trim($_POST['coupon_id'])) ? intval($_POST['coupon_id']) : 0;
			if($all == 2) {
				$sql = "SELECT id FROM jf_member where status = 1";
				$member = $this->db->get_results($sql);
				if($member){
					foreach ($member as $key => $va) {
					$sql = "UPDATE jf_coupon_sn SET member_id = {$va->id} ,get_time = '".time()."'  WHERE 
					get_time = 0 AND use_time = 0 AND coupon_id = {$coupon_id} LIMIT 1";
					$row = $this->db->query($sql);
					}
				}
			}else{
				$sql = "UPDATE jf_coupon_sn SET member_id = {$member_id} ,get_time = '".time()."'  WHERE 
					get_time = 0 AND use_time = 0 AND coupon_id = {$coupon_id} LIMIT 1";
				$row = $this->db->query($sql);	
			}
			
		if($row){
				echo"<meta charset='UTF-8'><script>alert('发放成功');window.location.href='?app=coupon&act=index';</script>";
			}else{
				echo"<meta charset='UTF-8'><script>alert('发放失败');window.location.href='?app=coupon&act=index';</script>";
			}
		}else{
			$time = time();
			$sql = "SELECT m.id,m.name FROM jf_member m WHERE 1  ";
			$member = $this->db->get_results($sql);
			$sql = "SELECT c.id ,c.name,c.coupon_money FROM jf_coupon c WHERE status = 1 AND end_time > {$time} ";
			$coupon = $this->db->get_results($sql);
			$this->smarty->assign('coupon',$coupon);
			$this->smarty->assign('member',$member);	
			$this->display();
		}
	}
}
